package com.myclub.web.Action;

import java.sql.DriverManager;
import java.sql.ResultSet;

import com.myclub.web.backingBean.AllMessage;
import com.myclub.web.backingBean.LoginBean;

public class LoginAction {
	private LoginBean loginBean;
	public AllMessage getAllMessage() {
		return allMessage;
	}
	public void setAllMessage(AllMessage allMessage) {
		this.allMessage = allMessage;
	}

	private AllMessage allMessage;
	public String sendMsg()
	{
		if(!loginBean.getMsg().equals(""))
		{
			allMessage.sendMsg(loginBean.getUser_id(), loginBean.getMsg());
			loginBean.setMsg("");
		}
		return "success";
	}
	public String logOut()
	{
		loginBean.setLoginFlg("0");
		loginBean.setUser_id("");
		loginBean.setPassword("");
		return "success";
	}

	public String action() {
		String driver = "com.mysql.jdbc.Driver"; // 驱动程序名
		String url = "jdbc:mysql://114.113.228.213:3309/myclub"; // URL指向要访问的数据库名,hhh是数据库名
		String user = "myclub"; // MySQL配置时的用户名
		String password = "gutmyclub"; // MySQL配置时的密码
		try {
			Class.forName(driver); // 加载驱动程序
			java.sql.Connection conn = DriverManager.getConnection(url, user,
					password); // 连接数据库
			if (!conn.isClosed())
				System.out.println("Succeeded connecting to the Database!"); // 验证是否连接成功
			java.sql.Statement statement = conn.createStatement(); // statement用来执行SQL语句
			String sql = "select * from m_user where user_id='"+loginBean.getUser_id()+"'"; // 要执行的SQL语句
			ResultSet rs = (ResultSet) statement.executeQuery(sql); // 结果集
			System.out.println(sql);
			while (rs.next()) {
				if (loginBean.getPassword().equals(rs.getString("password"))) {
					System.out.println("success");
					loginBean.setLoginFlg("1");
					return "success";
				}else{
					System.out.println("error");
					return "error";
				} 
			}

			rs.close();
			conn.close();
		}

		catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}
}
